<style>
.quantity_input {width:30px; text-align:right;}
.percent_input {width:20px; text-align:right;}
.price_input {width:50px; text-align:right;}
#checkout_items_button {float:right;}
#sales_comment{width:95%; height:40px; margin:5px 7px; border:1px solid #c3c3c4; resize:none;}
</style>
<div id="sales_table">
	<table id="sales_listing_tbl" cellpadding="0" cellspacing="0">
		<tr>
			<th width="50px">Item #</th>
			<th>Item Name</th>
			<th>Stock</th>
			<th>Qty</th>
			<th>Price</th>
			<th>Disc</th>
			<th>Total</th>
			<th width="50px">Remove</th>
		</tr>
		<?if($this->cart->total_items()):?>
			<?foreach($this->cart->contents() as $item):?>
				<tr>
					<td><?=str_pad($item['id'], 4, "0", STR_PAD_LEFT);?></td>
					<td><?=$item['name']?></td>
					<td><?=$item['inventory']?></td>
					<td><?=form_input(array('type'=>'text', 'name'=>'quantity', 'class'=>'quantity_input', 'rowid'=>$item['rowid'],'value'=>$item['qty']))?></td>
					<td>P <?=form_input(array('type'=>'text', 'name'=>'price', 'class'=>'price_input', 'rowid'=>$item['rowid'], 'value'=>number_format($item['price'],2,'.',',')))?></td>
					<td><?=form_input(array('type'=>'text', 'name'=>'disc', 'class'=>'percent_input', 'rowid'=>$item['rowid'], 'value'=>$item['disc']))?> %</td>
					<td>
						P <?=number_format(($item['price']-(($item['price']*$item['disc'])/100))*$item['qty'],2,'.',',')?>
					</td>
					<td><?=anchor("sales/update_item_cart/{$item['rowid']}/qty/0", '<img src="html/img/cancel_48.png" height="20px" width="20px"/>', 'class="delete_sales_item_button"')?></td>
				</tr>
			<?endforeach;?>
		<?else:?>
			<tr>
				<td colspan="8">No Items in Cart.</td>
			</tr>
		<?endif;?>
	</table>
</div>

<div id="sales_div">
	<div id="sales_summary">
		<div class="sales_breakdown">
			<table>
				<tr>
					<th colspan="2">A Cycle House</th>
				</tr>
				<tr>
					<td><b>Sales Summary:</b></td>
				</tr>
				<?$total_disc = 0;?>
				<?if($this->cart->total_items()):?>
					<?foreach($this->cart->contents() as $item):?>
					<tr>
						<td><?=$item['name']?> x <?=$item['qty'];?></td>
						<td>P <?=number_format($item['price']*$item['qty'],2,'.',',');?></td>
					</tr>
						<?if($item['disc']):?>
						<tr style="font-size:10px; font-style:italic;">
							<td><?=$item['disc']?>% Discount</td>
							<td>- P <?=number_format((($item['price']*$item['disc'])/100)*$item['qty'],2,'.',',')?></td>
						</tr>
						<?endif;?>
					<?$total_disc = $total_disc + (($item['price']*$item['disc'])/100)*$item['qty'];?>
					<?endforeach;?>
				<?else:?>
				<tr>
					<td colspan="2">No Items</td>
				</tr>
				<?endif;?>
			</table>
		</div>
		<div class="sales_total">
			<table>
				<tr>
					<th align="left">TOTAL: </th>
					<th>P <?=number_format($this->cart->total() - $total_disc,2,'.',',');?></th>
				</tr>
			</table>
		</div>
	</div>
<textarea id="sales_comment" name="sale_comment" placeholder="comment here.."></textarea>
<?= $this->cart->total_items() ? anchor('sales/cancel_sale/', 'Checkout', 'id="checkout_items_button"') : '';?>
</div>

<script>
	$(function() {
		$('#checkout_items_button').button({icons:{primary:'ui-icon-circle-check'}});

		$('.delete_sales_item_button').click(function(e){
			e.preventDefault();
			$.get($(this).attr('href'), function() {
				$('#sale_overview').load('sales/sales_table_update/');
			});
		});

		$('.quantity_input').change(function(){
			$.get('sales/update_item_cart/'+$(this).attr('rowid')+'/qty/'+$(this).val(), function(){
				$('#sale_overview').load('sales/sales_table_update/');
			});
		});

		$('.price_input').change(function(){
			$.get('sales/update_item_cart/'+$(this).attr('rowid')+'/price/'+$(this).val(), function(){
				$('#sale_overview').load('sales/sales_table_update/');
			});
		});

		$('.percent_input').change(function(){
			$.get('sales/update_item_cart/'+$(this).attr('rowid')+'/disc/'+$(this).val(), function(){
				$('#sale_overview').load('sales/sales_table_update/');
			});
		});
	});
</script>